/*
 * @Author: windys
 * @Description: 
 * @Date: 2021-12-03 14:05:24
 */
import type { RouteRecordRaw } from 'vue-router';
import { defineAsyncComponent } from 'vue'
import { $getMenuTree } from '../api/api';
import { permissionList } from './data'
import { Storage } from '../utils/storage';
const actions = {
    async GET_ROUTES({ commit }: any) {
        return new Promise(async (resolve, reject) => {
            const DynamicRoutes = filterAsyncRouter(permissionList);
            console.log(DynamicRoutes, '后台返回路由处理动态引入')
            // /* 完整的路由表 */
            commit("SET_ROUTES", DynamicRoutes);
            resolve(DynamicRoutes)
        })
    }
};




function filterAsyncRouter(permissionList: Array<any>): Array<any> {
    const modules = import.meta.glob('../pages/**/*.vue') //菜单
    const layouts = import.meta.glob('../layout/**/*.vue') //布局
    if (permissionList.length) {
        permissionList.forEach(el => {
            if (el.url.substring(0, 9) === '../layout') {
                el.component = layouts[el.url]
            } else {
                el.component = modules[el.url]
                el.meta = {
                    title: el.title,
                    keepAlive: el.keepAlive || false,
                    icon: el.icon || ''
                }
            }
            if (el.children && el.children.length) {
                return filterAsyncRouter(el.children)
            }
        })
    }
    return permissionList

}

export default actions;